//
//  UIView+x.m
//  AFNetworking
//
//  Created by baboy on 2019/2/16.
//

#import <Foundation/Foundation.h>
#import "UIView+x.h"
#import "BKEmptyMsgView.h"
#import <objc/runtime.h>

#define EMPTY_MSG_VIEW_TAG -99999999
#define TIP_FONT [UIFont systemFontOfSize:12]
#define TIP_BACKGROUND_COLOR [UIColor colorWithWhite:0.2 alpha:0.95]
#define TIP_TEXT_COLOR [UIColor colorWithWhite:0.95 alpha:1]

id loadViewFromNib(Class class, id owner){
    NSString *nibName = NSStringFromClass(class);
    NSArray *objs = [[NSBundle mainBundle] loadNibNamed:nibName
                                                  owner:owner
                                                options:nil];
    for (id v in objs) {
        if ([v isKindOfClass:class]) {
            return v;
        }
    }
    return nil;
}

@implementation UIView(x)
- (void)setUserInfo:(id)userInfo{
    objc_setAssociatedObject(self, @selector(userInfo), userInfo, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (id)userInfo{
    return objc_getAssociatedObject(self, @selector(userInfo));
}
- (UIImage *)screenshot{
    CGRect rect = self.bounds;
    UIGraphicsBeginImageContextWithOptions(rect.size, NO,0);
    CGContextRef currentContext = UIGraphicsGetCurrentContext();
    
    [self.layer renderInContext:currentContext];
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return img;
}
- (UIView *)showEmptyMsg:(NSString *)msg withNetIndicator:(BOOL)withIndicator icon:(NSString *)iconName withTarget:(id)target withAction:(SEL)action{
    BKEmptyMsgView *emptyMsgView = [self viewWithTag:EMPTY_MSG_VIEW_TAG];
    if (emptyMsgView) {
        [self removeEmptyIndicator];
    }
    emptyMsgView = [[BKEmptyMsgView alloc] initWithFrame:self.bounds withMessage:msg withNetworkIndicator:withIndicator icon:iconName];
    [emptyMsgView addTarget:target action:action];
    emptyMsgView.tag = EMPTY_MSG_VIEW_TAG;
    [self addSubview:emptyMsgView];
    return emptyMsgView;
}
- (UIView *)showEmptyMsg:(NSString *)msg icon:(NSString *)iconName withTarget:(id)target withAction:(SEL)action{
    return [self showEmptyMsg:msg withNetIndicator:NO icon:iconName withTarget:target withAction:action];
}
- (void)removeEmptyIndicator{
    UIView *v = [self viewWithTag:EMPTY_MSG_VIEW_TAG];
    [v removeFromSuperview];
}

//- (UIView *)showTips:(NSString*)tips frame:(CGRect)labelFrame duration:(float) duration{
//    UILabel *label = [[UILabel alloc] initWithFrame:labelFrame];
//    label.font = TIP_FONT;
//    label.backgroundColor = TIP_BACKGROUND_COLOR;
//    label.textColor = TIP_TEXT_COLOR;
//    label.layer.cornerRadius = labelFrame.size.height/2;
//    label.text = tips;
//    [self addSubview:label];
//    [UIView animateWithDuration:0.2
//                          delay:duration
//                        options:UIViewAnimationOptionTransitionNone
//                     animations:^{
//                         label.alpha = 0;
//                    }
//                     completion:^(BOOL finished) {
//                         [label removeFromSuperview];
//                     }];
//    return label;
//}
//- (UIView *)showTips:(NSString*)tips bottom:(CGFloat)height duration:(float) duration{
//    CGSize tipSize = [tips sizeWithAttributes:@{NSFontAttributeName:TIP_FONT}];
//    CGRect labelFrame = CGRectMake((self.bounds.size.width-tipSize.width-10)/2, self.bounds.size.height - height - tipSize.height-6, tipSize.width+10,tipSize.height+6);
//    return [self showTips:tips frame:labelFrame duration:duration];
//}
//- (UIView *)showTips:(NSString*)tips top:(CGFloat)height duration:(float) duration{
//    CGSize tipSize = [tips sizeWithAttributes:@{NSFontAttributeName:TIP_FONT}];
//    CGRect labelFrame = CGRectMake((self.bounds.size.width-tipSize.width-10)/2, height, tipSize.width+10,tipSize.height+6);
//    return [self showTips:tips frame:labelFrame duration:duration];
//    
//}
@end
